home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TEXTFILE.SWG / 0037_UNIX-Dos Text Converter.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-26  |  10KB  |  315 lines

  1. {
  2. I've gotten a couple requests for this source, which quasi-intelligently
  3. converts Unix-format text to DOS-format text and vice versa.  Recently,
  4. I just
  5. added a better command-line interpreter, and cleaned it up a little.  I was
  6. hoping to get around to using untyped files instead of text files, but maybe
  7. later.
  8.  
  9. This is probably not the most graceful (and since it uses text files,
  10. not the fastest way to do this), but it's worked well for me.
  11. Suggestions on how to improve are welcome.
  12.  
  13. -Scott E.
  14. tiobe@cmu.edu
  15. ------------------------------------------------------------------}
  16. program SConvert;
  17.  
  18. {Smart-converts UN*X/DOS format files
  19.  
  20.  Usage:  sconvert infile [outfile] [/U | /D]
  21.           [/U forces unix, /D forces DOS, if forced type, do nothing.]
  22.  
  23.          -- or --
  24.  
  25.          sconvert /?  (-?, /h, -h, /H, and -H analogous)
  26.           for help message
  27.  
  28.          This program is capabable of having its output piped, provided
  29.           it is the first in the pipeline.  Since it opens input twice,
  30.           using it anywhere in a pipe besides the beginning probably won't
  31.           work well.
  32.  
  33.  Written by Scott F. Earnest, Aug 1993
  34.  Original version:  30 Aug 1993
  35.  Updated version:    9 May 1994  (Added force flags.)
  36. }
  37.  
  38. uses Crt;
  39.  
  40. const
  41.   CR = chr(13);               {Carriage Return}
  42.   LF = chr(10);               {Line Feed}
  43.  
  44. type
  45.   sys = (dos,unix,bad);       {system identifier}
  46.  
  47. var
  48.   sysID : sys;                {system identifier for case branch}
  49.   infile, outfile : string;   {input/output files}
  50.   force : sys;                {What mode to work in.}
  51.  
  52. function exist (filename : string) : boolean;
  53.  
  54. {Check if a file exists or not
  55.  returns:  true  -->  file exists
  56.            false -->  file non-existent}
  57.  
  58. var
  59.   openfile : text;
  60.   errcode : integer;
  61.  
  62. begin
  63.   {$I-}                       {Turn off error-checking}
  64.   assign (openfile, filename);
  65.   reset (openfile);
  66.   {$I+}                       {Turn it back on}
  67.   errcode := IOResult;        {Get error code}
  68.   if  errcode <> 0  then      {There's an error if non-zero}
  69.     exist := false            {So flag that it doesn't exist.}
  70.   else
  71.     begin
  72.       close (openfile);       {Otherwise, close file}
  73.       exist := true;          {Flag that it does exist}
  74.     end;
  75. end;
  76.  
  77. function selectyn : boolean;
  78.  
  79. {Get a yes/no single-keypress response
  80.  returns:  true  -->  yes response, y or Y
  81.            false -->  no response, n or N}
  82.  
  83. var
  84.   getchar : char;             {Need something to read into}
  85.  
  86. begin
  87.   while KeyPressed do         {Clean keyboard buffer}
  88.     getchar := ReadKey;
  89.   repeat                      {Get a key until it's a (Y)es or (N)o.}
  90.     getchar := ReadKey;
  91.     getchar := upcase (getchar);
  92.   until (getchar in ['Y', 'N']);
  93.   writeln (getchar);          {Print the response}
  94.   case getchar of             {Tell it what it should return}
  95.     'Y' : selectyn := true;
  96.     'N' : selectyn := false;
  97.   end;
  98. end;
  99.  
  100. procedure help (badflag : boolean);
  101.  
  102. {brief message if command format was abused}
  103.  
  104. begin
  105.   writeln ('SmartConvert, Written by Scott F. Earnest -- v1.3 -- 9 May 1994');
  106.   writeln;
  107.   if badflag then
  108.     begin
  109.       writeln ('Invalid flag.');
  110.       writeln;
  111.     end;
  112.   writeln ('Usage');
  113.   writeln ('  sconvert infile [outfile] [/d | /u]');
  114.   writeln;
  115.   writeln ('Use /d to force conversion to DOS, and /u to force UNIX.');
  116.   halt (1);
  117. end;
  118.  
  119. procedure incheck (filename : string);
  120.  
  121. {Make sure source exists, if specified}
  122.  
  123. begin
  124.   if not (exist (filename)) then
  125.     begin
  126.       writeln ('Source file does not exist!');
  127.       halt (3);
  128.     end;
  129. end;
  130.  
  131. procedure outcheck (filename : string);
  132.  
  133. {Make sure target does NOT exist, if specified, allow overwrite}
  134.  
  135. var
  136.   select : boolean;
  137.  
  138. begin
  139.   if exist (filename) and (filename <> '') then
  140.     begin
  141.       write ('Target file exists!  Overwrite?  [y/n] ');
  142.       select := selectyn;
  143.       case select of
  144.         true : ;
  145.         false : halt (4);
  146.       end;
  147.     end;
  148. end;
  149.  
  150. function checktype (readfile : string) : sys;
  151.  
  152. var
  153.   FileCheck : text;
  154.   checkvar : sys;
  155.   CROk, LFOk : boolean;
  156.   ReadBuf : char;
  157.  
  158. begin
  159.   CROk := False;
  160.   LFOk := False;                        {Init flags.}
  161.   checkvar := bad;                      {Assume that type isn't known.}
  162.   assign (FileCheck, readfile);
  163.   reset (FileCheck);
  164.   while (not eof(FileCheck)) and (not CROk) and (not LFOk) do
  165.  
  166.     begin                               {Look for CR or LF}
  167.       read (FileCheck, ReadBuf);
  168.       if ReadBuf = CR then              {CR found?}
  169.         begin
  170.           CROk := True;                 {If yes, set the CR flag.}
  171.           Read (FileCheck, ReadBuf);    {and get next char}
  172.           if ReadBuf = LF then          {next one a LF?}
  173.             LFOk := True;               {Flag it as found.}
  174.           if CROk and LFOk then         {So is it CR/LF?}
  175.              begin
  176.                checktype := dos;        {If yes, specify DOS, and exit.}
  177.                close (FileCheck);
  178.                exit;
  179.              end;
  180.         end;
  181.       if ReadBuf = LF then              {Found a LF?}
  182.          begin
  183.            checktype := unix;           {If yes, assume unix.}
  184.            close (FileCheck);           {Close and exit.}
  185.            exit;
  186.          end;
  187.     end;
  188.   if checkvar = bad then                {If there was a problem:}
  189.     begin
  190.       writeln ('Ambiguous file type.  Can''t determine type.');
  191.       close (FileCheck);
  192.       halt(2);
  193.     end;
  194. end;
  195.  
  196. procedure dos2unix (infile, outfile : string);
  197.  
  198. var
  199.   intext, outtext : text;
  200.   ReadBuf1, ReadBuf2 : char;
  201.  
  202. begin
  203.   writeln ('Converting DOS -> UNIX. . . .');
  204.   assign (intext, infile);
  205.   reset (intext);
  206.   assign (outtext, outfile);
  207.   rewrite (outtext);
  208.   while not eof(intext) do
  209.     begin
  210.       read (intext, ReadBuf1);          {Get character}
  211.       if ReadBuf1 = CR then             {If it's CR then. . . }
  212.         begin
  213.           read (intext, ReadBuf2);      {. . . get next . . .}
  214.           if ReadBuf2 = LF then         {. . . and see if it's LF.}
  215.             write (outtext, LF)         {If yes, just put LF into new file.}
  216.           else
  217.             write (outtext, ReadBuf1, ReadBuf2); {Not CR/LF, dump to file.}
  218.         end
  219.       else
  220.         write (outtext, ReadBuf1);      {Dump the character to file.}
  221.     end;
  222.   close (intext);
  223.   close (outtext);
  224. end;
  225.  
  226. procedure unix2dos (infile, outfile : string);
  227.  
  228. var
  229.   intext, outtext : text;
  230.   ReadBuf : char;
  231.  
  232. begin
  233.   writeln ('Converting UNIX -> DOS. . . .');
  234.   assign (intext, infile);
  235.   reset (intext);
  236.   assign (outtext, outfile);
  237.   rewrite (outtext);
  238.   while not eof(intext) do
  239.     begin
  240.       read (intext, ReadBuf);           {Get a character.}
  241.       if ReadBuf = LF then              {Is it LF?}
  242.         write (outtext, CR+LF)          {If yes, put a CR/LF in its place.}
  243.       else
  244.         write (outtext, ReadBuf);       {Otherwise, replace the character.}
  245.     end;
  246.   close (intext);
  247.   close (outtext);
  248. end;
  249.  
  250. procedure getcommandline;
  251.  
  252. {get commandline info. . . .}
  253.  
  254. var
  255.   pnum : byte;                          {paramater counter}
  256.   pstr : string[2];                     {string snippet}
  257.   fname : string;                       {temporary string}
  258.  
  259. begin
  260.   if (paramcount < 1) or (paramcount > 3) then
  261.     help (false);                       {too few, too many--show help}
  262.   infile := '';                         {Init names.}
  263.   outfile := '';
  264.   force := bad;
  265.   for pnum := 1 to paramcount do        {Do this in two passes.}
  266.     begin                               {#1.)  Flags}
  267.       pstr := paramstr(pnum);           {Get parameter.}
  268.       pstr[2] := upcase(pstr[2]);
  269.       if pstr[1] in ['-', '/'] then     {Flag?}
  270.         case pstr[2] of  
  271.           'H', '?' : help (false);      {Is help.}
  272.           'D'      : force := dos;      {Is force DOS.}
  273.           'U'      : force := unix;     {Is force UNIX.}
  274.         else
  275.           help (true);                  {Bad switch.}
  276.         end;
  277.     end;
  278.   for pnum := 1 to paramcount do        {#2.)  Filenames}
  279.     begin  
  280.       fname := paramstr(pnum);          {Get parameter.}
  281.       if not (fname[1] in ['-', '/']) then
  282.         begin                           {If not flag then}
  283.           if infile = '' then           {Get infile}
  284.             infile := fname
  285.           else if (infile <> '') and (outfile = '') then
  286.             outfile := fname            {Get outfile}
  287.           else
  288.             help (false);               {Oops, too many.}
  289.         end;
  290.     end;
  291. end;
  292.  
  293. begin
  294.   getcommandline;                       {Parse parameters}
  295.   sysID := checktype (infile);          {Check the input file type}
  296.   if sysID = force then                 {If it's getting forced, then}
  297.     begin                               {compare types and skip if same.}
  298.       write ('Input file is already type ');
  299.       case sysID of
  300.         dos  : write ('DOS');
  301.         unix : write ('UNIX');
  302.       end;
  303.       writeln (', skipped.');
  304.       halt(5);
  305.     end;
  306.   case sysID of
  307.     dos : dos2unix (infile, outfile);    {DOS -> UNIX}
  308.     unix : unix2dos (infile, outfile);   {UNIX -> DOS}
  309.     bad : begin                          {Not likely to happen but. . . .}
  310.             writeln ('Internal error!  Check source code and recompile.');
  311.             halt (6);
  312.           end;
  313.   end;
  314. end.
  315.